// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
/**
 * 广告
 */
let base = require("panelBase")
let viewConfig = {
     coin:{
         node:null,
         name:"reward/coin",

     },
     effect:{
         node:null,
         name:"reward/effect",
     },
     cdLabel:{
         node:null,
         name:"reward/cd_bg/cd"
     },
     // 获得的奖励
     des:{
        node:null,
        name:"reward/des"
     },
     btn_reward_double:{
         node:null,
         name:"reward/btn_double",
         eventType:"click",
         cb:"onBtnRewardDoubleClick",
         active:false,
     },
     banner:{
         node:null,
         name:"content",
         active:false,
     },
     banner_name:{
         node:null,
         name:"content/banner_name"
     },
     banner_des:{
         node:null,
         name:"content/banner_des"
     },
     banner_btn:{
         node:null,
         name:"content/btn_double",
         eventType:"click",
         cb:"onBtnBannerClick"
     }

};
cc.Class({
    extends: base,

    properties: {
        countDown:null,
        curAd:"",
        cb:null,
        CD:10
    },

    init(){
        this._super(viewConfig);
        this.node.addComponent("myCountDown");
        this.countDown = this.node.getComponent("myCountDown");
        this.countDown.init(viewConfig['cdLabel'].node);
        cc.tg.eventCenter.addEvent("toShowAd",(data)=>{
           // cc.tg.adDataCenter.toShowAd(data);
           if(this.curAd != "")
                return ;
           this.curAd = data.codeId;
           if(data.cb)
           {
               this.cb = data.cb;
           }
           if(data.countDown)
           {
               this.CD = data.countDown;
           }
        });
        this.isInited = true;
        this.onHide();
    },
    onShow(){
        this._super();
        let finishCb = ()=>{
            this.onHide();
        }
        this.countDown.startCountDown(this.CD,null,finishCb,1,0);
        if(this.curAd != "")
        {
            cc.tg.adDataCenter.toShowAd(this.curAd);
        }
    },
    onBtnRewardDoubleClick(){
      //  this.onHide();
      cc.log("on click double");
    },
    onBtnBannerClick(){
        cc.log("on click banner");
    },

    onHide(){
        this._super();
        cc.tg.adDataCenter.toShowAd(this.curAd,true)
        this.curAd = "";
        if(this.cb)
        {
            this.cb();
        }
       // cc.tg.adDataCenter.toShowAd("sys_re_coin_ex",true);
    },
    onDestroy(){
        for(let key in viewConfig)
        {
            viewConfig[key].node = null;
        }
    },
});
